<?php
namespace app\admin\controller;

class Index extends BaseController
{
	public function _initialize() {
		parent::_initialize();
	}

    public function index()
    {
        return $this->fetch();
    }

    public function uploadBanner() {
    	$bannerConfig = model('Configuration')->where('key', 'banner_num')->find();
    	$bannerNum = (int)($bannerConfig->value);
    	$this->assign('bannerNum', $bannerNum);
    	$banners = model('BannerConf')->select();
    	$this->assign('banners', $banners);
    	// echo "<pre>";
    	// var_dump($banners);
    	// exit();
    	return $this->fetch();
    }

    public function getUploadInfo() {
	    $response = getOssPolicy();
	    echo json_encode($response);
    }

    public function uploadBannerInfo() {
    	$images = $_POST['images'];
    	$urlPaths = $_POST['urlPaths'];
    	$bannerConf = model('BannerConf');
    	$dataArr = array();
    	for ($i = 0, $count = count($images); $i < $count; $i++) {
    		$data = array('id' => $i+1, 'src' => $images[$i], 'url' => $urlPaths[$i]);
    		$dataArr[] = $data;
    	}
    	if ($bannerConf->saveAll($dataArr)) {
    		$this->success("提交成功", '/index.php/Admin/Index/index');
    	} else {
    		$this->error("提交失败", '/index.php/Admin/Index/uploadBanner');
    	}
    }


    public function authcode() {
    	return $this->fetch();
    }


    public function generateAuthCode() {
    	$code = getRandomCode(12);
    	$authCode = model('AuthCode');
    	$res = array('res_code' => 0);
    	if ($authCode->where('code', $code)->find()) {
    		$this->generateAuthCode();
    	} else {
    		$authCode->data([
    			'code' => $code,
    			'is_use' => 0
    		]);
    		if ($authCode->save()) {
    			$res['res_code'] = 1;
    			$res['code'] = $code;
    		}
    	}
    	echo json_encode($res);
    	exit();
    }


}
